import { Component } from "cc";
import { EntityVo } from "../vo/EntityVo";
import { ComponentType, EntityState, EntityType, GameConst } from "../vo/GameConst";
import { BaseComponent } from "../components/BaseComponent";
import { EntityPool } from "./EntityPool";

export class BaseEntity extends Component{
    protected _vo:EntityVo;
    protected _deltaTime: number = 0;
    protected _updateInterval: number = 0.5; 
    protected _type:EntityType;
    protected _isSelected:boolean = false;
    protected _isReset:boolean = false;
    protected _components:{[type:number]:BaseComponent} = {};
    private _customComponents: BaseComponent[] = [];

    protected onLoad(): void {
        this.init();
    }

    public addCustomComponent<T extends BaseComponent>(componentClass: new () => T): T {
        let component = this.getComponent(componentClass);
        if (!component) {
            component = this.addComponent(componentClass);
        }
        if (component && this._customComponents.indexOf(component) == -1) {
            this._customComponents.push(component);
            component.init();
            if (this._vo) {
                component.setData(this._vo);
            }
        }
        return component;
    }

    public removeCustomComponent<T extends BaseComponent>(componentClass: new () => T): void {
        const component = this.getComponent(componentClass);
        if (component) {
            const index = this._customComponents.indexOf(component);
            if (index !== -1) {
                this._customComponents.splice(index, 1);
            }
            component.stop(true);
            // this.removeComponent(componentClass);
        }
    }

    public getCustomComponent<T extends BaseComponent>(componentClass: new () => T): T {
        return this.getComponent(componentClass);
    }

    // public addCusComponent(type:ComponentType){
    //     let com = this._components[type];
    //     if(com) {
    //         if(com.vo){
    //             console.warn("重复添加:" + ComponentType[type]);
    //         }
    //         else{
    //             com.setData(this._vo);
    //         }
    //         return;
    //     }
    //     let comCls:any = GameConst.getComponentCls(type);
    //     if(!comCls) {
    //         console.error("未定义的component:" + ComponentType[type]);
    //         return;
    //     }
    //     com = this.node.addComponent(comCls) as BaseComponent;
    //     com.init();
    //     com.setData(this._vo);
    //     this._components[type] = com;
    // }

    // public removeCusComponent(type:ComponentType){
    //     let com = this._components[type];
    //     if(com){
    //         com.stop(true);
    //         delete this._components[type];
    //     }
    // }

    public setSelected(val:boolean){
        if(this._isSelected == val) return;
        this._isSelected = val;
        this.onSelectChanged();
    }

    public get isSelected():boolean{
        return this._isSelected;
    }

    public get type():EntityType{
        return this._type;
    }

    public get vo(){
        return this._vo;
    }

    protected update(dt: number): void {
        this._deltaTime += dt;
        if (!this.canUpdate()) return;
        this._deltaTime = 0;
        this.onUpdate(dt);
    }

    protected onUpdate(dt: number) { }

    protected canUpdate(): boolean {
        return this._deltaTime >= this._updateInterval;
    }

    public setData(vo:EntityVo) {
        this._vo = vo;
        this._type = vo.type;
        this._isReset = false;
        this._vo.setEntity(this);
        
        this._customComponents.forEach(component => {
            component.setData(this._vo);
        });
        
        this.updateVo();
        this.initComponent();
        this.onStateChanged(this._vo.state);
    }
   
    protected init() { }
    protected initComponent() { }
    protected updateVo() { }
    
    private onIdle() {
        this.playIdle();
    };
    private onWalk() {
        this.playWalk();
    };

    public onStateChanged(state: EntityState) { 
        switch (state) {
            case EntityState.idle:
                this.onIdle();
                break;
            case EntityState.walk:
                this.onWalk();
                break;
        }
    }
    protected onSelectChanged() { }
    protected playIdle() { }
    protected playWalk() { }

    // protected stopComponent(){
    //     for(let t in this._components){
    //         this._components[t].stop();
    //     }
    //     this._customComponents.forEach(component => {
    //         component.stop();
    //     });
    // }

    public reset(): void {
        this._isReset = true;
        // this.stopComponent();
        this._isSelected = false;
        EntityPool.recycleEntityObj(this._type,this.node);
        this._vo = null;
    }
}